#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <idetector.h>
#include <facetracker.h>
#include <qtimer.h>
#include <detectorfactory.h>

using namespace std;
using namespace cv;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(timerTick()));
    detectorFactory = new DetectorFactory();

    detector = detectorFactory->getDetector(detectorFactory->FACE_TRACKING);

   // Lire le flux vidéo de la caméra
   capture = cvCaptureFromCAM( -1 );

   if ( capture )
       timer->start(100);

}

void MainWindow::timerTick()
{
  Mat frame = cvQueryFrame( capture );

  if( !frame.empty() )
  {
      detector->detect(frame);

      ui->output1->setPixmap(QPixmap::fromImage(detector->getBaseImage()));
      ui->output2->setPixmap(QPixmap::fromImage(detector->getIntermediateImage()));
      ui->output3->setPixmap(QPixmap::fromImage(detector->getEndImage()));
  }
}

MainWindow::~MainWindow()
{
    delete ui;
    delete detector;
    delete timer;
    delete detectorFactory;
}

void MainWindow::on_detectionList_currentIndexChanged(int index)
{
    int type = detectorFactory->FACE_TRACKING;

    switch ( index )
    {
        case 0 :
            type = detectorFactory->FACE_TRACKING;
            printf ( "New detector : Face tracking" );
            break;
        case 1 :
            type = detectorFactory->CONTOUR_DETECTION;
            printf ( "New detector : Contour detection" );
            break;
        case 2 :
            type = detectorFactory->EDGE_DETECTION;
            printf ( "New detector : Edge detection" );
            break;
        case 3:
            type = detectorFactory->HAND_TRACKING;
            printf ( "New detector : Hand tracking" );
            break;
    }

    fflush(stdout);

    delete detector;
    detector = detectorFactory->getDetector(type);
}
